package com.lzit.smart_canteen.service;

import com.alibaba.fastjson2.JSONObject;
import com.lzit.smart_canteen.domain.Food;
import com.lzit.smart_canteen.repository.FoodRepository;
import com.lzit.smart_canteen.utils.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class FoodService {
    @Autowired
    private FoodRepository foodRepository;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    //查找窗口信息(已加入缓存)
    public List<Integer> queryWindow() {
        //先查询redis缓存
        Object object = redisTemplate.opsForValue().get("windowsInfo");
        if (object != null) {
            //缓存中存在，则直接返回
            return ListUtil.castList(object, Integer.class);
        } else {
            //缓存中不存在
            //先查询
            List<Integer> windows = foodRepository.findWindow();
            //不为空，再存入缓存
            if (!windows.isEmpty()) {
                redisTemplate.opsForValue().set("windowsInfo", windows, 1, TimeUnit.DAYS);
            }
            return windows;
        }
    }

    //按照窗口号查找食物(已加入缓存)
    public List<Food> queryFoodsByWindow(Integer window) {
        //先查询redis缓存
        Object object = redisTemplate.opsForValue().get("window:" + window);
//        System.out.println(object);
        if (object != null) {
            //缓存中存在，则直接返回
            return ListUtil.castList(object, Food.class);
        } else {
//            System.out.println("缓存中不存在");
            //缓存中不存在
            //先查询
            List<Food> foodList = foodRepository.findFoodsByWindow(window);
            //不为空，再存入缓存
            if (!foodList.isEmpty()) {
                redisTemplate.opsForValue().set("window:" + window, foodList, 1, TimeUnit.DAYS);
            }
            return foodList;
        }
    }

    //通过食物ID查找食物(已加入缓存)
    public Food queryFoodById(Integer id) {
        //先查询Redis缓存
        Object object = redisTemplate.opsForValue().get("food:" + id);
        if (object != null) {
            return (Food) object;
        } else {
            Food food = foodRepository.findFoodById(id);
            if (food != null) {
                redisTemplate.opsForValue().set("food:" + id, food, 1, TimeUnit.DAYS);
                return food;
            } else {
                return null;
            }
        }
    }

    //保存新的食物信息(已加入缓存)
    public void saveFood(Food food) {
        //先更新数据库
        foodRepository.save(food);
//        //再删除缓存
//        Integer window = food.getWindow();
//        JSONObject jsonObject = (JSONObject) redisTemplate.opsForValue().get("windowsInfo");
//        if (jsonObject!=null){
//            if (!jsonObject.containsKey(window)){
        redisTemplate.delete("windowsInfo");
//            }
//        }
    }

    //更新菜单上架状态(已加入缓存)
    public void updateStatusById(Integer id, String status) {
        //先更新数据库
        foodRepository.updateStatusById(id, status);
        //再删除缓存
        Food food = foodRepository.findFoodById(id);
        if (food != null) {
            Integer window = food.getWindow();
            redisTemplate.delete("window:" + window);
        }
    }
}
